from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
import uvicorn
from algorithm import task_reorganization
from utils import load_model

# 加载模型并记录日志
model = load_model("params/tsp_100")[0]

# 请求数据模型，定义二维点的结构
class RequestData(BaseModel):
    task_pos: List[List[float]]  # 每个点是[x, y]的格式,使用浮点数类型
    drone_pos: List[List[float]]  # 起点是[x, y]的格式,使用浮点数类型
    mode_type: str

app = FastAPI()

@app.post("/task_reorganization")
def sort_points(data: RequestData):
    try:
        # 直接调用，不使用 await
        result = task_reorganization(model, data.task_pos, data.drone_pos, data.mode_type)
    except Exception as e:
        return {"error": str(e)}
    return {"result": result}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8888)